package cn.com.jtang.wedding.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.jtang.wedding.model.Suggestion;
import cn.com.jtang.wedding.service.AdminUserManager;
import cn.com.jtang.wedding.service.SuggestionManager;

public class SuggestionAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SuggestionManager suggestionManager;
	private AdminUserManager adminUserManager;
	
	private Suggestion suggestion;
	private List<Suggestion> suggestionList;
	private List<Suggestion> suggestions;
	private String adviser;
	private String reply;

	private Long id;
	
	//添加新的建议
	public String addSuggestion(){
		suggestion.setAdminUserId(getCurrentAdminUser().getId());
		suggestion.setTime(new Date());
		suggestion.setIsReply(0);
		suggestionManager.add(suggestion);
		return SUCCESS;
	}
	
	//列出自己提交的建议列表 
	public String listSuggestion(){
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adminUserId", getCurrentAdminUser().getId());

		int count = suggestionManager.getCount(map);
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		suggestionList = suggestionManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum,30 );
		return SUCCESS;
	}
	
	/**
	 * 已回复自己的建议列表
	 * @return
	 */
	public String listRepliedSuggestion(){
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adminUserId", getCurrentAdminUser().getId());
		map.put("isReply", Long.valueOf(1));
		int count = suggestionManager.getCount(map);
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		suggestionList = suggestionManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum,30 );
		return SUCCESS;
	}
	
	
	//查看某一建议
	public String detailSuggestion(){
		if(id==null||id.equals(Long.valueOf(0))){
			return "fail";
		}
		suggestion=suggestionManager.queryById(id);
		return SUCCESS;
	}
	
	public String editSuggestion(){
		Suggestion suggestion1=suggestionManager.queryById(suggestion.getId());
		suggestion1.setContent(suggestion.getContent());
		suggestion1.setTime(new Date());
		suggestionManager.updateById(suggestion1.getId(), suggestion1);
		return SUCCESS;
	}
	
	public String deleteSuggestion(){
		if(id==null||id.equals(Long.valueOf(0))){
			return "fail";
		}
		suggestionManager.delete(id);
		return SUCCESS;
	}
	
	/**
	 * 管理员查看建议列表
	 */
	public String getAll(){
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		//Map<String, Object> map = new HashMap<String, Object>();
		//map.put("adminUserId", getCurrentAdminUser().getId());
		//int count = suggestionManager.getCount(map);
		int count = suggestionManager.getCount(null);
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		suggestions = suggestionManager.queryAll(null, pageSize * (pageNum - 1),
				pageSize * pageNum,30 );
		return SUCCESS;
	}
	/**
	 * 管理员查看某一建议
	 */
	public String getSingleSuggestion(){
		if(id==0){
			return "fail";
		}
		suggestion=suggestionManager.queryById(id);
		adviser = adminUserManager.queryById(suggestion.getAdminUserId()).getAdminUserName();
		return SUCCESS;
	}
	/**
	 * 管理员回复建议
	 */
	public String replySuggestion(){
		suggestion=suggestionManager.queryById(id);
		suggestion.setReply(reply);
		suggestion.setReplyTime(new Date());
		suggestion.setIsReply(1);
		suggestionManager.updateById(new Long((long)id), suggestion);
		return SUCCESS;
	}

	public SuggestionManager getSuggestionManager() {
		return suggestionManager;
	}

	public void setSuggestionManager(SuggestionManager suggestionManager) {
		this.suggestionManager = suggestionManager;
	}

	public Suggestion getSuggestion() {
		return suggestion;
	}

	public void setSuggestion(Suggestion suggestion) {
		this.suggestion = suggestion;
	}

	public List<Suggestion> getSuggestionList() {
		return suggestionList;
	}

	public void setSuggestionList(List<Suggestion> suggestionList) {
		this.suggestionList = suggestionList;
	}



	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<Suggestion> getSuggestions() {
		return suggestions;
	}

	public void setSuggestions(List<Suggestion> suggestions) {
		this.suggestions = suggestions;
	}

	public String getAdviser() {
		return adviser;
	}

	public void setAdviser(String adviser) {
		this.adviser = adviser;
	}

	public AdminUserManager getAdminUserManager() {
		return adminUserManager;
	}

	public void setAdminUserManager(AdminUserManager adminUserManager) {
		this.adminUserManager = adminUserManager;
	}

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}
	
	
}
